package com.itheima.core.web.controller;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itheima.common.utils.Page;

import com.itheima.core.po.Vegetable;

import com.itheima.core.service.VegetableService;
/**
 * 用户控制器类
 */
@Controller
public class VegetableController {
	// 依赖注入
	@Autowired
	private VegetableService vegetableService;

	
	

		// 客户来源

		/**
		 *  客户列表
		 */
		@RequestMapping(value = "/vegetable/list.action")
		public String list(@RequestParam(defaultValue="1")Integer page,
				@RequestParam(defaultValue="10")Integer rows, 
				String vegetable_id, String vegetable_name, String describes, String price,
				 Model model) {
			// 条件查询所有客户
			Page<Vegetable> vegetables = vegetableService
					.findVegetableList(page, rows,vegetable_id,vegetable_name, 
							describes, price);
			model.addAttribute("page", vegetables);
			// 客户来源
			model.addAttribute("vegetable_id", vegetable_id);
			model.addAttribute("vegetable_name", vegetable_name);
			model.addAttribute("describes", describes);
			model.addAttribute("price", price);
			return "vegetable";
		}
		
		@RequestMapping(value = "/order/list.action")
		public String list2(@RequestParam(defaultValue="1")Integer page,
				@RequestParam(defaultValue="10")Integer rows, 
				String vegetable_id, String vegetable_name, String describes, String price,
				 Model model) {
			// 条件查询所有客户
			Page<Vegetable> vegetables = vegetableService
					.findVegetableList(page, rows,vegetable_id,vegetable_name, 
							describes, price);
			model.addAttribute("page", vegetables);
			// 客户来源
			model.addAttribute("vegetable_id", vegetable_id);
			model.addAttribute("vegetable_name", vegetable_name);
			model.addAttribute("describes", describes);
			model.addAttribute("price", price);
			return "order";
		}
		
		@RequestMapping(value ="/vegetable/create.action")
		public String add() {
		    // 获取Session中的当前用户信息
		    return "createVegetable";
		    }
		
	/*	@RequestMapping(value ="/vegetable/craeteVegetable.action")
		@ResponseBody
		public String createVegetable(Vegetable vegetable) {
			vegetableService.createVegetable(vegetable);
		    return "redirect:list.action";
		    }*/
		@RequestMapping("/vegetable/createVegetable.action")
		public String vegetableCreate(Vegetable vegetable,HttpSession session) {
		    // 获取Session中的当前用户信息
		    Vegetable vegetable1 = (Vegetable) session.getAttribute("VEGETABLE_SESSION");
		    // 将当前用户id存储在客户对象中
		    vegetable.setVegetable_id(vegetable.getVegetable_id());
		    // 创建Date对象
		    Date date = new Date();
		    // 执行Service层中的创建方法，返回的是受影响的行数
		    int rows = vegetableService.createVegetable(vegetable);
		    if(rows > 0){
		        
		    }
		    return "redirect:list.action";
		}
		

		@RequestMapping("/vegetable/getVegetableById.action")
		public String getVegetableById(String vegetable_id,Model model) {
			Vegetable vegetable = vegetableService.getVegetableById(vegetable_id);
			model.addAttribute("vegetable", vegetable);
		    return "updateVegetable";
		}
		
		
		/**
		 * 更新客户
		 */
		@RequestMapping(value ="/vegetable/update.action")
		public String update() {
		    // 获取Session中的当前用户信息
		    return "updateVegetable";
		    }
		
		@RequestMapping("/vegetable/updateVegetable.action")
		public String vegetableUpdate(Vegetable vegetable) {
		    int rows = vegetableService.updateVegetable(vegetable);
		    if(rows > 0){
		    }
		    return "redirect:list.action";
		}

		/**
		 * 删除客户
		 */
		@RequestMapping("/vegetable/delete.action")
		public String vegetableDelete(String vegetable_id) {
		    vegetableService.deleteVegetable(vegetable_id);
		    return "redirect:list.action";
		}
}

